package com.lsh.oms.core.service.sms;

import com.alibaba.fastjson.JSON;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.model.order.OrderShippingHead;
import com.lsh.oms.core.model.order.OrderSignHead;
import com.lsh.oms.core.model.sms.SmsModel;
import com.lsh.oms.core.service.asynservice.OmsEventBus;
import com.lsh.oms.core.service.other.ShippingOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 17/3/28
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service
@Slf4j
public class SmsService {

    @Autowired
    private ShippingOrderService shippingService;

    @Value("${smsUrl}")
    private String smsUrl;

    public void toSignSMS(OrderSignHead orderSignHead, OrderHead orderHead, String logSign) {
        try {
            OrderShippingHead orderShippingHead = shippingService.getBolbs(orderHead.getOrderCode());

            log.info("orderShippingHead is " + JSON.toJSONString(orderShippingHead));
            Map<String, Object> params = new HashMap<>(15);
            params.put("arrivedAt", orderShippingHead.getArrivedAt());
            params.put("money", orderSignHead.getMoney());
            params.put("receiptOrderId", orderSignHead.getReceiptOrderId());
            params.put("receiptAt", orderSignHead.getReceiptAt());
            params.put("shippingOrderId", orderSignHead.getShippingOrderId());
            params.put("orderCode", orderHead.getOrderCode());
            params.put("uid", orderHead.getUserCode());
            params.put("zoneId", orderHead.getRegionCode());
            params.put("addressId", orderHead.getAddressCode());
            params.put("venderId", orderHead.getVenderId());

            OmsEventBus.post(new SmsModel(params, smsUrl, logSign));
        } catch (Throwable e) {
            log.error(logSign + ",发送SMS失败，orderCode:" + orderHead.getOrderCode());
            log.error(e.getMessage(), e);
        }
    }


}
